<?php

/**
 * 员工
 *
 * @package		Hooloo framework
 * @author 		Bill
 * @copyright 	Hooloo Co.,Ltd
 * @version		1.0
 * @release		2017.05.08
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Staff extends Controller {
	public function __construct() {
		parent::__construct();
	}

	/**
     * 列表
     */
	public function index($aid = 0, $sear = 0, $p = 1) {
		
		//指定代理商
		if ($aid == 0) {
			$where = " where a.aid > 0";
		} else {
			$where = " where a.aid = $aid";
		}
		
		//存在搜索
		if ($sear) {
			$where .= " and a.username like '%$sear%'";
		} else {
			$sear = 0;
		}
		
		//数据分页
		$pize = 15;//每页数量
		if (0 >= $p) {
			$p = 1;
		}
		$start = ($p - 1) * $pize;
		
		//查询总数
		$sql = "select count(*) as c from agent_staff" . str_replace("a.", "", $where);
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select a.*, b.agent_name, b.username bname from agent_staff a inner join agent b on a.aid = b.id " . $where . " order by id desc limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . '/' . $this->_action . '/' . $aid . "/". $sear . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//查询所有代理商
		$sql = "select * from agent";
		$agent = $this->db->query($sql)->result_array();
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("agent",$agent);
		$this->assign("page", $page);
		$this->assign("sear", $sear);
		$this->assign("aid", $aid);
		$this->assign("total", $total);
		$this->display();
	}
	
	/**
     * 新增
	 * @param	string	$username	姓名
     * @param	int		$aid 		代理商ID
     * @param	string	$sex 		性别
     * @param	int		$mobile 	手机号
     * @param	array	$result 	返回结果
     */
	public function add() {
		if (IS_POST) {
			//接收参数
			$username = isset($_POST["username"]) ? substr(sql_format($_POST["username"]), 0, 20) : "";
			$sex = isset($_POST["sex"]) ? mb_substr(sql_format($_POST["sex"]), 0, 2) : "";
			$aid = isset($_POST["aid"]) ? (int)$_POST["aid"] : 0;
			$mobile = isset($_POST["mobile"]) ? sql_format($_POST["mobile"]) : 0;
			
			//校验参数
			if (0 >= $aid) {
				ajax_return(0, "参数有误");
			} else {
				$sql = "select id from agent where id = $aid";
				$res = $this->db->query($sql)->row_array();
				if (! $res) {
					ajax_return(0, "所属代理商不存在");
				}
			}
			if (! $username) {
				ajax_return(0, "姓名不能为空");
			} else {
				//查询用用户名是否已经存在
				$sql = "select username from agent_staff where username = '$username' and aid = $aid";
				$res = $this->db->query($sql)->row_array();
				if ($res) {
					ajax_return(0, "姓名已存在");
				}
			}
			if (! $sex) {
				ajax_return(0, "性别不能为空");
			}
			if (! $mobile) {
				ajax_return(0, "手机号不能为空");
			} else {
				if (! chk_phone($mobile)) {
					ajax_return(0, "手机号不正确");
				} else {
					//查询用手机号是否已经存在
					$sql = "select mobile from agent_staff where mobile = '$mobile' and aid = $aid";
					$res = $this->db->query($sql)->row_array();
					if ($res) {
						ajax_return(0, "手机号已存在");
					}
				}
			}
			
			//插入员工信息
			$reg_date = time();
			$sql = "insert into agent_staff (aid, username, sex, mobile) values ($aid, '$username', '$sex', $mobile)";
			$this->db->query($sql);
			$res =$this->db->insert_id();
			if (! $res) {
				ajax_return(0, "添加失败");
			} else {
				ajax_return(1, "添加成功", "/staff");
			}
		}
		
		//查询所有代理商
		$sql = "select * from agent where is_locked = 0";
		$agent = $this->db->query($sql)->result_array();
		$this->assign("agent",$agent);
		$this->display();
	}
	
	/**
     * 编辑
	 * @param	string	$username	姓名
     * @param	int		$aid 		代理商ID
     * @param	string	$sex 		性别
     * @param	int		$mobile 	手机号
     * @param	array	$result 	返回结果
     */
	public function edit($id = 0) {
		if (IS_POST) {
			//接收参数
			$username = isset($_POST["username"]) ? substr(sql_format($_POST["username"]), 0, 20) : "";
			$sex = isset($_POST["sex"]) ? mb_substr(sql_format($_POST["sex"]), 0, 2) : "";
			$aid = isset($_POST["aid"]) ? (int)$_POST["aid"] : 0;
			$mobile = isset($_POST["mobile"]) ? sql_format($_POST["mobile"]) : 0;
			
			//校验参数
			if (0 >= $id) {
				ajax_return(0, "参数有误(100)");
			} else {
				$sql = "select username from agent_staff where id = $id";
				$agent_staff = $this->db->query($sql)->row_array();
				if (! $agent_staff) {
					ajax_return(0, "员工不存在");
				} else {
					$old_username = $agent_staff["username"];
				}
			}
			if (0 >= $aid) {
				ajax_return(0, "参数有误(101)");
			} else {
				$sql = "select id from agent where id = $aid";
				$res = $this->db->query($sql)->row_array();
				if (! $res) {
					ajax_return(0, "所属代理商不存在");
				}
			}
			if (! $username) {
				ajax_return(0, "姓名不能为空");
			} else {
				//查询用用户名是否已经存在
				$sql = "select username from agent_staff where username = '$username' and aid = $aid and id != $id";
				$res = $this->db->query($sql)->row_array();
				if ($res) {
					ajax_return(0, "姓名已存在");
				}
			}
			if (! $sex) {
				ajax_return(0, "性别不能为空");
			}
			if (! $mobile) {
				ajax_return(0, "手机号不能为空");
			} else {
				if (! chk_phone($mobile)) {
					ajax_return(0, "手机号不正确" . $mobile);
				} else {
					//查询用手机号是否已经存在
					$sql = "select mobile from agent_staff where mobile = '$mobile' and aid = $aid and id != $id";
					$res = $this->db->query($sql)->row_array();
					if ($res) {
						ajax_return(0, "手机号已存在");
					}
				}
			}
			
			//更新员工信息
			$sql = "update agent_staff set aid = $aid, username = '$username', sex = '$sex', mobile = '$mobile' where id = $id";
			$this->db->query($sql);
			
			//更新用户表员工名字
			if ($username != $old_username) {
				$sql = "update user set staff_name = '$username' where sid = $id";
				$this->db->query($sql);
			}
			ajax_return(1, "更新成功", "/staff");
		} else {
			if (0 >= $id) {
				$this->error("参数有误");
			}
			//员工信息
			$sql = "select * from agent_staff where id = $id";
			$result = $this->db->query($sql)->row_array();
			if (! $result) {
				$this->error("员工不存在");
			}
			
			//代理商信息
			$sql = "select * from agent";
			$agent = $this->db->query($sql)->result_array();
			
			$this->assign("agent",$agent);
			$this->assign("result", $result);
			$this->display();
		}
	}
	
	/**
     * 员工下属会员列表
     */
	public function user($sid = 0, $demo_account = 2, $username = 0, $p = 1) {
		if ($sid <= 0) {
			$this->error("非法请求");
		}
		
		//指定员工
		$where = " where sid = $sid";
		
		//帐号类型
		if ($demo_account == 0) {
			$where .= " and demo_account = 0";
		} elseif ($demo_account == 1) {
			$where .= " and demo_account = 1";
		} else {
			$demo_account = 2;
		}
		
		//存在搜索
		if ($username) {
			$where .= " and (username like '%$username%' or realname like '%$username%')";
		} else {
			$username = 0;
		}
		
		//数据分页
		$pize = 15;//每页数量
		if (0 >= $p) {
			$p = 1;
		}
		$start = ($p - 1) * $pize;
		
		//查询总数
		$sql = "select count(*) as c from user" . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select * from user " . $where . " order by id desc limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . '/' . $this->_action . '/' . $sid . "/" . $demo_account . "/" . $username . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("sid", $sid);
		
		$this->assign("demo_account", $demo_account);
		$this->assign("username", $username);
		$this->display();
	}
	
	/**
     * 员工下属会员列表
     */
	public function user_bill($sid = 0, $demo_account = 2, $username = 0, $p = 1) {
		if ($sid <= 0) {
			$this->error("非法请求");
		}
		
		//指定员工
		$where = " where a.sid = $sid";
		
		//帐号类型
		if ($demo_account == 0) {
			$where .= " and a.demo_account = 0";
		} elseif ($demo_account == 1) {
			$where .= " and a.demo_account = 1";
		} else {
			$demo_account = 2;
		}
		
		//存在搜索
		if ($username) {
			$where .= " and (a.username like '%$username%' or a.realname like '%$username%')";
		} else {
			$username = 0;
		}
		
		//数据分页
		$pize = 15;//每页数量
		if (0 >= $p) {
			$p = 1;
		}
		$start = ($p - 1) * $pize;
		
		//查询总数
		$sql = "select count(a.id) as c from user a inner join log_bill b on a.id = b.uid " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//分页查询
		$sql = "select a.username, a.realname, a.agent_name, b.* from user a inner join log_bill b on a.id = b.uid " . $where . " order by b.id desc limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . '/' . $this->_action . '/' . $sid . "/" . $demo_account . "/" . $username . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("sid", $sid);
		
		$this->assign("demo_account", $demo_account);
		$this->assign("username", $username);
		$this->display();
	}
}
